package zhklappoint.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import zhklappoint.dao.CompanyDao;
import zhklappoint.entity.Company;
import zhklappoint.enums.ENUM_ISDELETE;
import zhklappoint.service.InsertIntoCompanyInfoService;
import zhklappoint.util.PageUtils;

@Service
public class InsertIntoCompanyInfoServiceImpl  implements InsertIntoCompanyInfoService{

	@Autowired
	private CompanyDao cd;
	
	@Override
	public Company saveCompany(Company company) {
		
		return cd.save(company);
	}

	@Override
	public void deleteCompany(Company company) {
		company.setIsdelete(ENUM_ISDELETE.DELETED.value);
		cd.save(company);
	}

	@Override
	public Page<Company> getCompanyList(Company company, Integer page, Integer size) {
		company.setIsdelete(ENUM_ISDELETE.NOT_DELETED.value);
        Pageable pageable= PageUtils.toPageable(page, size, Sort.Direction.DESC, "createdate");
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withMatcher("isdelete", ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("companyName", ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("bossName", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("bossPhome", ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("status", ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("payStatus", ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("id", ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("wxUserId", ExampleMatcher.GenericPropertyMatchers.contains())
                .withIgnorePaths("createdate","updatedate");// 忽略字段，即不管忽略名字是什么值都不加入查询条件
        Example<Company> example = Example.of(company, matcher);
        return cd.findAll(example, pageable);
	}

	//次查询必须是公司名称不能为空
	@Override
	public Page<Company> getCompanyListByMore(Company company, Integer page, Integer size) throws Exception {
		Pageable pageable = PageUtils.toPageable(page, size, Sort.Direction.DESC, "createdate");
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("isdelete", ENUM_ISDELETE.NOT_DELETED.value);
		StringBuffer queryString = new StringBuffer("select * from zhkl_appoint_company zac");
		queryString.append(" WHERE zac.isdelete = :isdelete ");
		
		if(!ObjectUtils.isEmpty(company.getCompanyName())) {
			values.put("company_name", company.getCompanyName());
			queryString.append(" and (zac.company_name    LIKE '%' :company_name '%'");
		}else {
			if(!ObjectUtils.isEmpty(company.getBossPhome())) {
				throw new Exception("参数错误");
			}
		}
		if(!ObjectUtils.isEmpty(company.getBossName())) {
			values.put("boss_name", company.getBossName());
			queryString.append(" or zac.boss_name    LIKE '%' :boss_name '%')");
		}
		if(!ObjectUtils.isEmpty(company.getBossPhome())) {
			values.put("boss_phome", company.getBossPhome());
			queryString.append(" or zac.boss_phome    LIKE '%' :boss_phome '%')");
		}
		queryString.append(" ORDER BY status ASC,createdate DESC ");
		StringBuffer countString = new StringBuffer("select count(1) from (").append(queryString).append(") a");
		return cd.sqlQuery(queryString+"", countString+"", values, pageable,company.getClass());
	}

	@Override
	public Company getCompanyById(String id) {
		Optional<Company> ou=cd.findById(id);
        if(!ou.isPresent()) {
            return null;
        }
        Company c=ou.get();
        if(c.getIsdelete()==ENUM_ISDELETE.DELETED.value){
            return null;
        }
        return c;
	}

	
}
